Skip to content

Method: addPlayer(SspPlayer, SspStrategy)

1: /*
2: * Copyright © 2021-2023 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel24-Ssp.
5: *
6: * Ipspiel24-Ssp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
7: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
8: * version.
9: *
10: * Ipspiel24-Ssp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
11: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12: *
13: * You should have received a copy of the GNU General Public License along with ipspiel24-Ssp. If not, see
14: * <http://www.gnu.org/licenses/>.
15: */
16: package de.fhdw.gaming.ipspiel24.ssp.domain.impl;
17:
18: import java.util.LinkedHashMap;
19: import java.util.Map;
20: import java.util.Objects;
21: import java.util.Optional;
22:
23: import de.fhdw.gaming.core.domain.DefaultGame;
24: import de.fhdw.gaming.core.domain.DefaultObserverFactoryProvider;
25: import de.fhdw.gaming.core.domain.Game;
26: import de.fhdw.gaming.core.domain.GameBuilder;
27: import de.fhdw.gaming.core.domain.GameException;
28: import de.fhdw.gaming.core.domain.ObserverFactoryProvider;
29: import de.fhdw.gaming.ipspiel24.ssp.domain.SspGameBuilder;
30: import de.fhdw.gaming.ipspiel24.ssp.domain.SspPlayer;
31: import de.fhdw.gaming.ipspiel24.ssp.domain.SspPlayerBuilder;
32: import de.fhdw.gaming.ipspiel24.ssp.domain.SspState;
33: import de.fhdw.gaming.ipspiel24.ssp.domain.SspStrategy;
34: import de.fhdw.gaming.ipspiel24.ssp.moves.SspMove;
35: import de.fhdw.gaming.ipspiel24.ssp.moves.impl.AbstractSspMove;
36:
37: /**
38: * Implements {@link SspGameBuilder}.
39: */
40: final class SspGameBuilderImpl implements SspGameBuilder {
41:
42: /**
43: * The {@link ObserverFactoryProvider}.
44: */
45: private ObserverFactoryProvider observerFactoryProvider;
46: /**
47: * The player using black tokens.
48: */
49: private Optional<SspPlayer> firstPlayer;
50: /**
51: * The strategy of the player using black tokens.
52: */
53: private Optional<SspStrategy> firstPlayerStrategy;
54: /**
55: * The player using white tokens.
56: */
57: private Optional<SspPlayer> secondPlayer;
58: /**
59: * The strategy of the player using white tokens.
60: */
61: private Optional<SspStrategy> secondPlayerStrategy;
62: /**
63: * The maximum computation time per move in seconds.
64: */
65: private int maxComputationTimePerMove;
66:
67: /**
68: * Creates a Ssp game builder.
69: */
70: SspGameBuilderImpl() {
71: this.observerFactoryProvider = new DefaultObserverFactoryProvider();
72: this.firstPlayer = Optional.empty();
73: this.firstPlayerStrategy = Optional.empty();
74: this.secondPlayer = Optional.empty();
75: this.secondPlayerStrategy = Optional.empty();
76: this.maxComputationTimePerMove = GameBuilder.DEFAULT_MAX_COMPUTATION_TIME_PER_MOVE;
77: }
78:
79: @Override
80: public SspPlayerBuilder createPlayerBuilder() {
81: return new SspPlayerBuilderImpl();
82: }
83:
84: @Override
85: public SspGameBuilder addPlayer(final SspPlayer player, final SspStrategy strategy)
86: throws GameException {
87:
88:• if (this.firstPlayer.isEmpty()) {
89: this.firstPlayer = Optional.of(Objects.requireNonNull(player, "player"));
90: this.firstPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "firstPlayerStrategy"));
91:• } else if (this.secondPlayer.isEmpty()) {
92: this.secondPlayer = Optional.of(Objects.requireNonNull(player, "player"));
93: this.secondPlayerStrategy = Optional.of(Objects.requireNonNull(strategy, "secondPlayerStrategy"));
94: } else {
95: throw new GameException(String.format("More than two players are now allowed."));
96: }
97: return this;
98: }
99:
100: @Override
101: public SspGameBuilder changeMaximumComputationTimePerMove(final int newMaxComputationTimePerMove) {
102: this.maxComputationTimePerMove = newMaxComputationTimePerMove;
103: return this;
104: }
105:
106: @Override
107: public SspGameBuilder changeObserverFactoryProvider(final ObserverFactoryProvider newObserverFactoryProvider) {
108: this.observerFactoryProvider = newObserverFactoryProvider;
109: return this;
110: }
111:
112: @Override
113: public Game<SspPlayer, SspState, SspMove, SspStrategy> build(final int id)
114: throws GameException, InterruptedException {
115: if (!this.firstPlayer.isPresent() || !this.secondPlayer.isPresent()) {
116: throw new GameException("A Ssp game needs two players.");
117: }
118:
119: final SspStateImpl initialState = new SspStateImpl(this.firstPlayer.get(), this.secondPlayer.get());
120:
121: final Map<String, SspStrategy> strategies = new LinkedHashMap<>();
122: strategies.put(initialState.getFirstPlayer().getName(), this.firstPlayerStrategy.orElseThrow());
123: strategies.put(initialState.getSecondPlayer().getName(), this.secondPlayerStrategy.orElseThrow());
124: return new DefaultGame<>(
125: id,
126: initialState,
127: strategies,
128: this.maxComputationTimePerMove,
129: AbstractSspMove.class::isInstance,
130: new SspMoveGeneratorImpl(),
131: this.observerFactoryProvider);
132: }
133: }